// Copyright (c) 2023 Presto Labs Pte. Ltd.
// Author: tianqiang

#include "coin2/exchange/base/order/system_critical_error.h"

#include <cstdlib>

#include "coin2/base/log.h"

namespace coin2::exchange::base::order {

void SystemCriticalError::Log(
    const char* file,
    int line,
    std::string_view condition,
    std::string_view message) {
  if (callback_set_.empty()) {
    LOG(ERROR) << "Unexpect emtpy callback set, please contact OG team! file: " << file
               << " line: " << line << " condition: " << condition << " message: " << message;

    std::abort();
  }

  for (const auto* callback : callback_set_) {
    (*callback)(file, line, condition, message);
  }
}

bool SystemCriticalError::RegisterCallback(Callback* callback) {
  return callback_set_.insert(callback).second;
}

bool SystemCriticalError::UnregisterCallback(Callback* callback) {
  return callback_set_.erase(callback);
}

SystemCriticalError& SystemCriticalError::Get() {
  static SystemCriticalError system_critical_error;

  return system_critical_error;
}

}  // namespace coin2::exchange::base::order
